package com.ca.demo.service.sys.impl;

import com.ca.demo.common.base.BaseServiceImpl;
import com.ca.demo.config.security.SecurityUser;
import com.ca.demo.mapper.master.sys.ResourceMapper;
import com.ca.demo.model.sys.Resource;
import com.ca.demo.model.sys.tmp.Menu;
import com.ca.demo.service.sys.ResourceService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * @Author XieLei
 * @Date 2017/6/22 17:44
 */
@Service
public class ResourceServiceImpl extends BaseServiceImpl<Resource, Long> implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public List<Resource> findChildren(Resource resource) {
        if (resource == null) {
            resource = new Resource();
        }
        if (resource.getParentId() == null) {
            resource.setParentId(0L);
        }
        PageHelper.orderBy("weight desc");
        List<Resource> root = resourceMapper.select(resource);

        return null;
    }

    @Override
    public List<Resource> findAll(Resource resource) {
        PageHelper.orderBy("weight asc");
        return resourceMapper.select(resource);
    }

    @Override
    public List<Menu> findMenus(SecurityUser user) {
        PageHelper.orderBy("parent_id desc, weight desc");
        List<Resource> resources = resourceMapper.selectAll();
        return convertToMenus(resources);
    }

    private static List<Menu> convertToMenus(List<Resource> resources) {
        if (CollectionUtils.isEmpty(resources)) {
            return Collections.EMPTY_LIST;
        }
        Menu root = convertToMenu(resources.remove(resources.size() - 1));

        recursiveMenu(root, resources);
        List<Menu> menus = root.getChildren();
        removeNoLeafMenu(menus);

        return menus;
    }

    private static void removeNoLeafMenu(List<Menu> menus) {
        if (menus.size() == 0) {
            return;
        }
        for (int i = menus.size() - 1; i >= 0; i--) {
            Menu m = menus.get(i);
            removeNoLeafMenu(m.getChildren());
            if (!m.isHasChildren() && StringUtils.isEmpty(m.getUrl())) {
                menus.remove(i);
            }
        }
    }

    private static void recursiveMenu(Menu menu, List<Resource> resources) {
        for (int i = resources.size() - 1; i >= 0; i--) {
            Resource resource = resources.get(i);
            if (resource.getParentId().equals(menu.getId())) {
                menu.getChildren().add(convertToMenu(resource));
                resources.remove(i);
            }
        }

        for (Menu subMenu : menu.getChildren()) {
            recursiveMenu(subMenu, resources);
        }
    }

    private static Menu convertToMenu(Resource resource) {
        return new Menu(resource.getId(), resource.getName(), resource.getIcon(), resource.getUrl());
    }
}
